<template>
  <div>
    <el-row type=flex>
      <el-col :span="16">
        <el-button @click="handleCreate" icon="el-icon-plus" type="primary">新建红包</el-button>
      </el-col>
      <el-col :span="8">
        <el-input placeholder="请输入红包名称" v-model="filterForm.title">
          <el-button @click="handleFilter" slot="append" icon="el-icon-search"></el-button>
        </el-input>
      </el-col>
    </el-row>

    <el-table :data="reward.list" stripe class="hui-mt-1">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-form :model="scope.row" label-width="8em" label-position="left" inline class="hui-data-table-expand">
            <el-form-item label="红包标题:">{{ scope.row.title }}</el-form-item>
            <el-form-item label="红包类型:">
              <el-tag v-if="scope.row.luckyType === 'STATIC'" size="small">固定金额</el-tag>
              <el-tag v-else-if="scope.row.luckyType === 'RANDOM'" size="small">限定范围随机</el-tag>
              <el-tag v-else-if="scope.row.luckyType === 'ENUM'">限定金额随机</el-tag>
              <el-tag v-else size="small">{{ scope.row.luckyType }}</el-tag>
            </el-form-item>
            <el-form-item label="红包金额:">
              <template v-if="scope.row.luckyType === 'STATIC'">
                立减 {{ scope.row.reduceCost / 100 | currency('', 2) }} 元
                <template v-if="scope.row.leastCost === 0">，无门槛</template>
                <template v-else>，满 {{ scope.row.leastCost / 100 | currency('', 2) }} 元可用</template>
              </template>
              <template v-else-if="scope.row.luckyType === 'RANDOM'">
                {{ scope.row.reduceCostMin / 100 | currency('', 2) }} 至 {{ scope.row.reduceCostMax / 100 | currency('', 2) }} 元
                <template v-if="scope.row.leastCost === 0">，无门槛</template>
                <template v-else>，满 {{ scope.row.leastCost / 100 | currency('', 2) }} 元可用</template>
              </template>
              <template v-else-if="scope.row.luckyType === 'ENUM'">
                <el-table :data="scope.row.reduceEnum" border stripe size="small" style="width: 320px; line-height: 23px;">
                  <el-table-column label="红包金额">
                    <template slot-scope="lu">
                      立减 {{ lu.row.reduceCost / 100 | currency('', 2) }} 元
                    </template>
                  </el-table-column>
                  <el-table-column label="起用金额">
                    <template slot-scope="lu">
                      <template v-if="lu.row.leastCost === 0">无门槛</template>
                      <template v-else>满 {{ lu.row.leastCost / 100 | currency('', 2) }} 元可用</template>
                    </template>
                  </el-table-column>
                </el-table>
              </template>
            </el-form-item>
            <el-form-item label="红包数量:" v-if="scope.row.luckyType !== 'RANDOM'">
              <div>总库存：{{ scope.row.quantity }} 个</div>
              <div>还剩余：{{ scope.row.stock }} 个</div>
            </el-form-item>
            <el-form-item label="红包总金额:" v-else-if="scope.row.luckyType === 'RANDOM'">
              <div>总金额：{{ scope.row.reduceCostTotal / 100 | currency('', 2) }} 元</div>
              <div>还剩余：{{ scope.row.reduceCostStock / 100 | currency('', 2) }} 元</div>
            </el-form-item>
            <el-form-item label="使用日期限制:">{{ scope.row.useAllWeekly ? '所有日期' : '一周内的某些天' }}</el-form-item>
            <el-form-item label="可使用日期:" v-if="!scope.row.useAllWeekly">
              <el-tag v-for="item in scope.row.canUseWeekly" :key="item" type="primary" size="small" class="hui-mr-8px">
                <template v-if="item === 1">周一</template>
                <template v-else-if="item === 2">周二</template>
                <template v-else-if="item === 3">周三</template>
                <template v-else-if="item === 4">周四</template>
                <template v-else-if="item === 5">周五</template>
                <template v-else-if="item === 6">周六</template>
                <template v-else-if="item === 7">周日</template>
              </el-tag>
            </el-form-item>
            <el-form-item label="使用时段限制:">{{ scope.row.useAllDaily ? '所有时段' : '一天内的某些时段' }}</el-form-item>
            <el-form-item label="可使用时段:" v-if="!scope.row.useAllDaily">
              <el-tag v-for="item in scope.row.canUseDaily" :key="item" type="primary" size="small" class="hui-mr-8px">
                <template v-if="item === 0">0点 - 1点</template>
                <template v-else-if="item === 1">1点 - 2点</template>
                <template v-else-if="item === 2">2点 - 3点</template>
                <template v-else-if="item === 3">3点 - 4点</template>
                <template v-else-if="item === 4">4点 - 5点</template>
                <template v-else-if="item === 5">5点 - 6点</template>
                <template v-else-if="item === 6">6点 - 7点</template>
                <template v-else-if="item === 7">7点 - 8点</template>
                <template v-else-if="item === 8">8点 - 9点</template>
                <template v-else-if="item === 9">9点 - 10点</template>
                <template v-else-if="item === 10">10点 - 11点</template>
                <template v-else-if="item === 11">11点 - 12点</template>
                <template v-else-if="item === 12">12点 - 13点</template>
                <template v-else-if="item === 13">13点 - 14点</template>
                <template v-else-if="item === 14">14点 - 15点</template>
                <template v-else-if="item === 15">15点 - 16点</template>
                <template v-else-if="item === 16">16点 - 17点</template>
                <template v-else-if="item === 17">17点 - 18点</template>
                <template v-else-if="item === 18">18点 - 19点</template>
                <template v-else-if="item === 19">19点 - 20点</template>
                <template v-else-if="item === 20">20点 - 21点</template>
                <template v-else-if="item === 21">21点 - 22点</template>
                <template v-else-if="item === 22">22点 - 23点</template>
                <template v-else-if="item === 23">23点 - 24点</template>
              </el-tag>
            </el-form-item>
            <el-form-item label="订单限制:">{{ scope.row.originalFeeOnly ? '所有订单' : '仅限原价订单享受' }}</el-form-item>
            <el-form-item label="领红包打标签:">
              <el-tag v-for="item in scope.row.userTagsGet" :key="item.id" size="small" class="hui-mr-8px"># {{ item.label }} #</el-tag>
            </el-form-item>
            <el-form-item label="用红包打标签:">
              <el-tag v-for="item in scope.row.userTagsConsumed" :key="item.id" size="small" class="hui-mr-8px"># {{ item.label }} #</el-tag>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="红包">
        <template slot-scope="scope">
          <div>{{ scope.row.title }}</div>
          <div>
            <el-tag v-if="scope.row.status === 1" type="success" size="small">正常</el-tag>
            <el-tag v-else-if="scope.row.status === 2" type="info" size="small">已过期</el-tag>
            <el-tag v-else-if="scope.row.status === 3" type="danger" size="small">已删除</el-tag>
            <el-tag v-else type="danger" size="small">{{ scope.row.status }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="有效期">
        <template slot-scope="scope">
          <template v-if="scope.row.dateInfoType === 'DATE_TYPE_FIX_TIME_RANGE'">
            <div>开始时间: {{ $moment.unix(scope.row.beginTimestamp).format("YYYY-MM-DD") }}</div>
            <div>结束时间: {{ $moment.unix(scope.row.endTimestamp).format("YYYY-MM-DD") }}</div>
          </template>
          <template v-else-if="scope.row.dateInfoType === 'DATE_TYPE_FIX_TERM'">
            <div>领取后 {{ scope.row.fixedBeginTerm === 0 ? '当' : scope.row.fixedBeginTerm }} 天生效</div>
            <div>有效期 {{ scope.row.fixedTerm }} 天</div>
            <div v-if="scope.row.endTimestamp">统一过期时间: {{ $moment.unix(scope.row.endTimestamp).format("YYYY-MM-DD") }}</div>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="最后操作人">
        <template slot-scope="scope">
          <div>{{ scope.row.updateUser }}</div>
          <div>{{ $moment.unix(scope.row.updateTime).format("YYYY-MM-DD HH:mm:ss") }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200px">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row)" type="primary" size="mini" :disabled="scope.row.status === 2 || scope.row.status === 3" plain>编辑</el-button>
          <el-button @click="handleDelete(scope.row)" :disabled="scope.row.status === 2 || scope.row.status === 3" type="danger" size="mini" plain>删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页，start-->
    <el-pagination @current-change="handleCurrentChange" :current-page.sync="reward.pages" :page-sizes="[10]" :page-size="10" layout="total, sizes, prev, pager, next" :total="reward.total" background class="hui-mt-2"></el-pagination>
    <!--分页，end-->
  </div>
</template>

<script>
export default {
  data () {
    return {
      filterForm: {
        title: ''
      },
      loading: false,
      reward: {
        pages: 0,
        total: 0,
        list: []
      }
    }
  },
  created () {
    this.mockData()
  },
  methods: {
    // 筛选事件
    handleFilter () {
      if (this.filterForm.title === '') {
        return false
      } else {
        this.loading = true
        console.log(this.filterForm)
        this.$loading()
        setTimeout(() => {
          this.$loading().close()
          this.loading = false
        }, 500)
      }
    },
    // 新建事件
    handleCreate () {
      this.$router.push({
        path: 'detail',
        query: {
          type: 'create'
        }
      })
    },
    // 编辑事件
    handleEdit (row) {
      let id = row.id
      this.$router.push({
        path: 'detail',
        query: {
          type: 'edit',
          id: id
        }
      })
    },
    // 删除事件
    handleDelete (row) {
      this.$confirm('确认删除这个红包么？删除后，本批次已经发给用户的红包仍可继续使用，但无法再发新的券。', '删除确认', {
        confirmButtonText: '下线',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log('下线, ID:' + row.id)
        this.$message({
          type: 'success',
          message: '已删除!'
        })
      }).catch(() => {})
    },
    // 分页事件
    handleCurrentChange () {
      console.log(this.reward.pages)
      this.$loading()
      setTimeout(() => {
        this.$loading().close()
      }, 500)
    },
    // 模拟数据
    mockData () {
      let pages = this.reward.pages
      let total = 100
      let list = []
      let i = 10
      while (i > 0) {
        let _startTime = (val) => {
          return parseInt(this.$moment().day(val).set({'hour': 0, 'minute': 0, 'second': 0}).format('X'))
        }
        let _endTime = (val) => {
          return parseInt(this.$moment().day(val).set({'hour': 23, 'minute': 59, 'second': 59}).format('X'))
        }
        let _status = (min, max) => {
          min = Math.ceil(min)
          max = Math.floor(max)
          return Math.floor(Math.random() * (max - min + 1)) + min
        }
        let _luckyType = () => {
          const type = ['STATIC', 'RANDOM', 'ENUM']
          const n = Math.floor(Math.random() * type.length + 1) - 1
          return type[n]
        }
        let _updateTime = () => {
          return parseInt(this.$moment().format('X'))
        }
        list.push({
          id: i,
          status: _status(1, 3),
          updateTime: _updateTime(),
          weixinCard: i % 2 !== 0,
          title: '红包标题' + i,
          startTime: _startTime(1),
          endTime: _endTime(1 + i),
          updateUser: '黄倬, 13262268525',
          luckyType: _luckyType(),
          reduceCost: 16,
          quantity: 5000,
          stock: 2000,
          reduceCostMin: 100,
          reduceCostMax: 1000,
          reduceCostTotal: 1000000,
          reduceCostStock: 500000,
          leastCost: i % 2 !== 0 ? 2000 : 0,
          reduceEnum: [{reduceCost: 16, leastCost: 2000}, {reduceCost: 28, leastCost: 3000}],
          dateInfoType: i % 2 !== 0 ? 'DATE_TYPE_FIX_TERM' : 'DATE_TYPE_FIX_TIME_RANGE',
          fixedTerm: 30,
          fixedBeginTerm: 1,
          beginTimestamp: 1486857600,
          endTimestamp: 1489334399,
          useAllWeekly: i % 2 !== 0,
          canUseWeekly: [1, 2, 3, 4, 5],
          useAllDaily: i % 2 !== 0,
          canUseDaily: [11, 12, 13, 14],
          originalFeeOnly: i % 2 !== 0,
          useAllBranch: i % 2 !== 0,
          canUseBranch: [{id: 1, label: '门店名称1'}, {id: 2, label: '门店名称2'}],
          userTagsGet: i % 2 !== 0 ? [] : [{id: i, label: '标签名称' + i}, {id: i + 1, label: '标签名称' + (i + 1)}],
          userTagsConsumed: i % 2 !== 0 ? [] : [{id: i, label: '标签名称' + i}]
        })
        i--
      }
      this.reward.list = list
      this.reward.pages = pages + 1
      this.reward.total = total
      console.table(this.reward.list)
    }
  }
}
</script>
